<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="库名" prop="dbName">
        <el-input
          v-model="queryParams.dbName"
          placeholder="请输入库名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="表名" prop="tableName">
        <el-input
          v-model="queryParams.tableName"
          placeholder="请输入表名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="表描述" prop="tableDesc">
        <el-input
          v-model="queryParams.tableDesc"
          placeholder="请输入表描述"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="表注释" prop="tableComment">
        <el-input
          v-model="queryParams.tableComment"
          placeholder="请输入表注释"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['db:table:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['db:table:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['db:table:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['db:table:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tableList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="库名" align="center" prop="dbName" />
      <el-table-column label="表名" align="center" prop="tableName" />
      <el-table-column label="表描述" align="center" prop="tableDesc" />
      <el-table-column label="数据库引擎" align="center" prop="tableEngine" />
      <el-table-column label="自增开始值" align="center" prop="tableAutoIncrement" />
      <el-table-column label="字符集" align="center" prop="tableCharset" />
      <el-table-column label="表注释" align="center" prop="tableComment" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['db:table:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['db:table:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
        	<el-col :span="12">
            	<el-form-item label="库名称" prop="dbName">
              		<el-input v-model="form.dbName" placeholder="请输入库名" />
            	</el-form-item>
            </el-col>
        	<el-col :span="12">
            	<el-form-item label="表名" prop="tableName">
              		<el-input v-model="form.tableName" placeholder="请输入表名" />
            	</el-form-item>
            </el-col>
        </el-row>
        <el-row>
        	<el-col :span="12">
            	<el-form-item label="表描述" prop="tableDesc">
              		<el-input v-model="form.tableDesc" placeholder="请输入表描述" />
            	</el-form-item>
            </el-col>
        	<el-col :span="12">
            	<el-form-item label="数据库引擎" prop="tableEngine">
              		<el-input v-model="form.tableEngine" placeholder="请输入数据库引擎" />
            	</el-form-item>
            </el-col>
        </el-row>
        <el-row>
        	<el-col :span="12">
            	<el-form-item label="自增开始值" prop="tableAutoIncrement">
              		<el-input v-model="form.tableAutoIncrement" placeholder="请输入自增开始值" />
            	</el-form-item>
            </el-col>
        	<el-col :span="12">
            	<el-form-item label="字符集" prop="tableCharset">
              		<el-input v-model="form.tableCharset" placeholder="请输入字符集" />
            	</el-form-item>
            </el-col>
        </el-row>
        <el-row>
        	<el-col :span="12">
            	<el-form-item label="表注释" prop="tableComment">
              		<el-input v-model="form.tableComment" placeholder="请输入表注释" />
            	</el-form-item>
            </el-col>
        	<el-col :span="12">
            	<el-form-item label="备注" prop="remark">
              		<el-input v-model="form.remark" placeholder="请输入内容" />
            	</el-form-item>
            </el-col>
        </el-row>
        <el-divider content-position="center">数据库模型之信息信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddDbTableColumn">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteDbTableColumn">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="dbTableColumnList" :row-class-name="rowDbTableColumnIndex" @selection-change="handleDbTableColumnSelectionChange" ref="dbTableColumn">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="列名" prop="columnName" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.columnName" placeholder="请输入列名" />
            </template>
          </el-table-column>
          <el-table-column label="列类型" prop="columnType" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.columnType" placeholder="请选择列类型">
                <el-option label="请选择字典生成" value="" />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="列长度" prop="columnLength" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.columnLength" placeholder="请输入列长度" />
            </template>
          </el-table-column>
          <el-table-column label="非空约束" prop="columnNotnull" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.columnNotnull" placeholder="请输入非空约束" />
            </template>
          </el-table-column>
          <el-table-column label="自动记录时间(Y代表1,N代表否)" prop="columnAutotime" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.columnAutotime" placeholder="请输入自动记录时间(Y代表1,N代表否)" />
            </template>
          </el-table-column>
          <el-table-column label="默认值" prop="columnDefault" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.columnDefault" placeholder="请输入默认值" />
            </template>
          </el-table-column>
          <el-table-column label="列注释" prop="columnComment" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.columnComment" placeholder="请输入列注释" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTable, getTable, delTable, addTable, updateTable } from "@/api/db/table";

export default {
  name: "Table",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedDbTableColumn: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 数据库模型之信息表格数据
      tableList: [],
      // 数据库模型之信息表格数据
      dbTableColumnList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dbName: null,
        tableName: null,
        tableDesc: null,
        tableEngine: null,
        tableAutoIncrement: null,
        tableCharset: null,
        tableComment: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询数据库模型之信息列表 */
    getList() {
      this.loading = true;
      listTable(this.queryParams).then(response => {
        this.tableList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        dbName: null,
        tableName: null,
        tableDesc: null,
        tableEngine: null,
        tableAutoIncrement: null,
        tableCharset: null,
        tableComment: null,
        status: "0",
        delFlag: null,
        createId: null,
        createBy: null,
        createTime: null,
        updateId: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.dbTableColumnList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加数据库表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTable(id).then(response => {
        this.form = response.data;
        this.dbTableColumnList = response.data.dbTableColumnList;
        this.open = true;
        this.title = "修改数据库表";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.dbTableColumnList = this.dbTableColumnList;
          if (this.form.id != null) {
            updateTable(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTable(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除数据库模型之信息编号为"' + ids + '"的数据项？').then(function() {
        return delTable(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 数据库模型之信息序号 */
    rowDbTableColumnIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 数据库模型之信息添加按钮操作 */
    handleAddDbTableColumn() {
      let obj = {};
      obj.columnName = "";
      obj.columnType = "";
      obj.columnLength = "";
      obj.columnNotnull = "";
      obj.columnAutotime = "";
      obj.columnDefault = "";
      obj.columnComment = "";
      this.dbTableColumnList.push(obj);
    },
    /** 数据库模型之信息删除按钮操作 */
    handleDeleteDbTableColumn() {
      if (this.checkedDbTableColumn.length == 0) {
        this.$modal.msgError("请先选择要删除的数据库模型之信息数据");
      } else {
        const dbTableColumnList = this.dbTableColumnList;
        const checkedDbTableColumn = this.checkedDbTableColumn;
        this.dbTableColumnList = dbTableColumnList.filter(function(item) {
          return checkedDbTableColumn.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleDbTableColumnSelectionChange(selection) {
      this.checkedDbTableColumn = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('db/table/export', {
        ...this.queryParams
      }, `table_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
